package cn.wellcare.service;

import cn.wellcare.dao.SysCodeMasterDao;
import cn.wellcare.entity.SysCodeMaster;
import cn.wellcare.exception.BusinessException;
import cn.wellcare.exception.ErrorEnum;
import cn.wellcare.pojo.ServiceResponse;
import cn.wellcare.utils.CommonUtils;
import cn.wellcare.utils.PagerInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "sysCodeMasterService")
public class SysCodeMasterService{
	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource
	private SysCodeMasterDao sysCodeMasterDao;
    
     /**
     * 根据id取得数据字典
     * @param  codeDiv
	  * @param  codeCd
     * @return
     */
    public ServiceResponse<SysCodeMaster> getSysCodeMasterById(String codeDiv, String codeCd) {
        ServiceResponse<SysCodeMaster> serviceResult = new ServiceResponse<SysCodeMaster>();
        try {
            serviceResult.setData(sysCodeMasterDao.get(codeDiv,codeCd));
        } catch (Exception e) {
            log.error("[ISysCodeMasterService][getSysCodeMasterById]根据id["+codeCd+"]取得数据字典时出现未知异常：",
                e);
			if (e instanceof BusinessException) {
				BusinessException pe = (BusinessException) e;
				if (pe.getCode() != null && ErrorEnum.BUSINESS_EXCEPTION.getErrCode().equals(pe.getCode()))
					serviceResult.setMsgInfo(ErrorEnum.BUSINESS_EXCEPTION.getErrDesc());
				else
					serviceResult.setMsgInfo(e.getMessage());
				serviceResult.setMsgCode(ErrorEnum.BUSINESS_EXCEPTION.getErrCode());
			} else {
				e.printStackTrace();
				serviceResult.setError(ErrorEnum.SERVER_EXCEPTION);
			}
			throw e;
        }
        return serviceResult;
    }
    
    /**
     * 保存数据字典
     * @param  sysCodeMaster
     * @return
     */
     public ServiceResponse<Integer> saveSysCodeMaster(SysCodeMaster sysCodeMaster) {
     	ServiceResponse<Integer> serviceResult = new ServiceResponse<Integer>();
        try {
			sysCodeMaster.setCreateTime(new Date());
			this.dbConstrains(sysCodeMaster);
            serviceResult.setData(sysCodeMasterDao.save(sysCodeMaster));
        } catch (Exception e) {
            log.error("[ISysCodeMasterService][saveSysCodeMaster]保存数据字典时出现未知异常：",
                e);
			if (e instanceof BusinessException) {
				BusinessException pe = (BusinessException) e;
				if (pe.getCode() != null && ErrorEnum.BUSINESS_EXCEPTION.getErrCode().equals(pe.getCode()))
					serviceResult.setMsgInfo(ErrorEnum.BUSINESS_EXCEPTION.getErrDesc());
				else
					serviceResult.setMsgInfo(e.getMessage());
				serviceResult.setMsgCode(ErrorEnum.BUSINESS_EXCEPTION.getErrCode());
			} else {
				e.printStackTrace();
				serviceResult.setError(ErrorEnum.SERVER_EXCEPTION);
			}
			throw e;
        }
        return serviceResult;
     }
     
     /**
     * 更新数据字典
     * @param  sysCodeMaster
     * @return
     * @see SysCodeMasterService#updateSysCodeMaster(SysCodeMaster)
     */
     public ServiceResponse<Integer> updateSysCodeMaster(SysCodeMaster sysCodeMaster) {
     	ServiceResponse<Integer> serviceResult = new ServiceResponse<Integer>();
        try {
			sysCodeMaster.setUpdateTime(new Date());
			this.dbConstrains(sysCodeMaster);
            serviceResult.setData(sysCodeMasterDao.update(sysCodeMaster));
        } catch (Exception e) {
            log.error("[ISysCodeMasterService][updateSysCodeMaster]更新数据字典时出现未知异常：",
                e);
			if (e instanceof BusinessException) {
				BusinessException pe = (BusinessException) e;
				if (pe.getCode() != null && ErrorEnum.BUSINESS_EXCEPTION.getErrCode().equals(pe.getCode()))
					serviceResult.setMsgInfo(ErrorEnum.BUSINESS_EXCEPTION.getErrDesc());
				else
					serviceResult.setMsgInfo(e.getMessage());
				serviceResult.setMsgCode(ErrorEnum.BUSINESS_EXCEPTION.getErrCode());
			} else {
				e.printStackTrace();
				serviceResult.setError(ErrorEnum.SERVER_EXCEPTION);
			}
			throw e;
        }
        return serviceResult;
     }
     
     
    public ServiceResponse<List<SysCodeMaster>> page(Map<String, Object> queryMap, PagerInfo pager) {
        ServiceResponse<List<SysCodeMaster>> serviceResult = new ServiceResponse<List<SysCodeMaster>>();
        try {
			Map<String, Object> param = new HashMap<String, Object>(queryMap);
			Integer start = 0, size = 0;
			if (pager != null) {
				pager.setRowsCount(sysCodeMasterDao.getCount(param));
				start = pager.getStart();
				size = pager.getPageSize();
			}
			param.put("start", start);
			param.put("size", size);
			serviceResult.setData(sysCodeMasterDao.getList(param));
        } catch (Exception e) {
            log.error("[SysCodeMasterService][page] exception:" + e.getMessage());
            
			if (e instanceof BusinessException) {
				BusinessException pe = (BusinessException) e;
				if (pe.getCode() != null && ErrorEnum.BUSINESS_EXCEPTION.getErrCode().equals(pe.getCode()))
					serviceResult.setMsgInfo(ErrorEnum.BUSINESS_EXCEPTION.getErrDesc());
				else
					serviceResult.setMsgInfo(e.getMessage());
				serviceResult.setMsgCode(ErrorEnum.BUSINESS_EXCEPTION.getErrCode());
			} else {
				e.printStackTrace();
				serviceResult.setError(ErrorEnum.SERVER_EXCEPTION);
			}
		
        }
        return serviceResult;
    }

    public ServiceResponse<Boolean> del(String codeDiv,String codeCd) {
 		ServiceResponse<Boolean> serviceResult = new ServiceResponse<Boolean>();
        try {
            serviceResult.setData(sysCodeMasterDao.del(codeDiv,codeCd) > 0);
        } catch (Exception e) {
            log.error("[SysCodeMasterService][del] exception:" + e.getMessage());
            
			if (e instanceof BusinessException) {
				BusinessException pe = (BusinessException) e;
				if (pe.getCode() != null && ErrorEnum.BUSINESS_EXCEPTION.getErrCode().equals(pe.getCode()))
					serviceResult.setMsgInfo(ErrorEnum.BUSINESS_EXCEPTION.getErrDesc());
				else
					serviceResult.setMsgInfo(e.getMessage());
				serviceResult.setMsgCode(ErrorEnum.BUSINESS_EXCEPTION.getErrCode());
			} else {
				e.printStackTrace();
				serviceResult.setError(ErrorEnum.SERVER_EXCEPTION);
			}
			throw e;
        }
        return serviceResult;
    }

	private void dbConstrains(cn.wellcare.entity.SysCodeMaster sysCodeMaster) {
		sysCodeMaster.setCodeCd(CommonUtils.dbSafeString(sysCodeMaster.getCodeCd(), false, 16));
		sysCodeMaster.setCodeDiv(CommonUtils.dbSafeString(sysCodeMaster.getCodeDiv(), false, 16));
		sysCodeMaster.setCodeText(CommonUtils.dbSafeString(sysCodeMaster.getCodeText(), true, 16));
	}
}